# -*- coding: utf-8 -*-
import sys
import time
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class MainUi(QWidget):
    def __init__(self):
        super(MainUi, self).__init__()
        self.resize(400, 200)
        self.setWindowTitle('QThread例子')

        # 实例化多线程对象
        self.thread = Worker()

        # 实例化列表控件与按钮控件
        self.listFile = QListWidget()
        self.btnStart = QPushButton('开始')
        self.btn_over = QPushButton('结束')

        # 把控件放置在栅格布局中
        layout = QGridLayout(self)
        layout.addWidget(self.listFile, 0, 0, 1, 2)
        layout.addWidget(self.btnStart, 1, 0)
        layout.addWidget(self.btn_over, 1, 1)

        # 信号与槽函数的连接
        self.btnStart.clicked.connect(self.slotStart)
        self.btn_over.clicked.connect(self.slot_btn_over)

        # 建立线程信号的槽连接
        self.thread.trigger.connect(self.slotAdd)

    def slotAdd(self, msg):
        print(msg)
        if int(msg) % 2 == 0:
            self.listFile.addItem(msg)
        else:
            pass
        self.thread.exit()

    def slotStart(self):
        self.btnStart.setEnabled(False)
        self.thread.start()

    def slot_btn_over(self):
        self.btnStart.setEnabled(True)
        self.thread.terminate()
        self.thread.num = 0


class Worker(QThread):
    trigger = pyqtSignal(str)
    num = 0

    def __init__(self):
        super(Worker, self).__init__()

    def run(self):
        while True:
            print('num= ', self.num)
            if self.num % 2 == 0:
                self.trigger.emit(str(50))
            elif self.num == 200:
                self.num = 0
            else:
                pass
            time.sleep(0.1)
            self.num += 1


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainUi()
    w.show()
    sys.exit(app.exec_())
